package com.doit.hbase.client;

import com.doit.hbase.utils.HbaseUtils;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.*;

import java.util.Iterator;

/**
 * @DATE 2021/12/13/14:48
 * @Author MDK
 * @Version 2021.2.2
 *  Scan全表扫描
 *      多行数据   ResultScanner  迭代器
 *      ---------> Iterator<Result>
 *      Result 一行数据   行里面有单元格数据
 *
 **/
public class C04_Scan {
    public static void main(String[] args) throws Exception {
        Connection connection = HbaseUtils.getConnection();
        Table table = HbaseUtils.getTable("tb_teacher", connection);

        //扫描所有数据
        Scan scan = new Scan();
        //ResultScanner存储的是多行数据
        ResultScanner scanner = table.getScanner(scan);
        //Result  行数据,while遍历
        Iterator<Result> iterator = scanner.iterator();
        while (iterator.hasNext()){
            System.out.println("-----------------行数据----------------------");
            //每行中有不同个数的单元格
            Result result = iterator.next();
            //行中有多个单元格  行键  列族  属性  值 --->Cell
            //每行数据的单元格由迭代器遍历  判断是否还有下一个单元格
            while (result.advance()){
                //获取当前单元格
                Cell cell = result.current();
                //CellUtil提供了cell的解析工具类
                byte[] row = CellUtil.cloneRow(cell);
                byte[] family = CellUtil.cloneFamily(cell);
                byte[] qualifier = CellUtil.cloneQualifier(cell);
                byte[] value = CellUtil.cloneValue(cell);

                System.out.println(new String(row)+":"+new String(family)+":"+
                                   new String(qualifier)+":"+new String(value));
            }

        }

        //释放资源
        table.close();
        connection.close();
    }
}
